package com.houtai.controller.dict;

import com.houtai.authority.Authority;
import com.houtai.authority.AuthorityType;
import com.houtai.base.RetInfo;
import com.houtai.bean.TsJzAdmin;
import com.houtai.bean.TsJzDict;
import com.houtai.bean.dto.dict.TsJzDictListPage;
import com.houtai.bean.dto.dict.TsJzDictParentListPage;
import com.houtai.service.dict.DictService;
import com.houtai.service.funcion.FuncionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping(value = {"v5"})
public class DictController {
    @Resource
    private DictService dictService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private FuncionService funcionService;
    /**
     * @Purpose   获取所有子数据
     * @param     parent_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/dict/son/all", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo all(Integer parent_id, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        retInfo.setMark("0");
        retInfo.setObj(dictService.findAllDictByParentId(parent_id));
        return retInfo;
    }
    /**
     * @Purpose  分页加载数据
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/dict/son/list")
    @Authority(AuthorityType.Login)
    public ModelAndView son(HttpServletRequest request, TsJzDictParentListPage page){
        ModelAndView mv = new ModelAndView("dict/son");
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("retInfo", dictService.findDictByParentId(page, request));
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("father_id", request.getParameter("father_id"));
        mv.addObject("funcions", funcionService.findSonFuncion(tsJzAdmin, Integer.valueOf(request.getParameter("father_id"))));
        return mv;
    }
    /**
     * @Purpose  分页加载数据
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/dict/list")
    @Authority(AuthorityType.Login)
    public ModelAndView list(HttpServletRequest request, TsJzDictListPage page){
        ModelAndView mv = new ModelAndView("dict/list");
        TsJzAdmin tsJzAdmin = (TsJzAdmin) redisTemplate.opsForValue().get(request.getParameter("token")) ;
        mv.addObject("retInfo", dictService.findDictList(page, request));
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("father_id", request.getParameter("father_id"));
        mv.addObject("funcions", funcionService.findSonFuncion(tsJzAdmin, Integer.valueOf(request.getParameter("father_id"))));
        return mv;
    }
    /**
     * @Purpose   删除数据
     * @param     dict_id
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/dict/delete", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo delete(Integer dict_id, HttpServletRequest request) {
        return dictService.deleteDict(dict_id, request);
    }
    /**
     * @Purpose  跳转到更新数据页面
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/dict/to/update")
    @Authority(AuthorityType.Login)
    public ModelAndView index(HttpServletRequest request, Integer dict_id){
        ModelAndView mv = new ModelAndView("dict/update");
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("dict", dictService.findTsJzDictById(dict_id));
        return mv;
    }
    /**
     * @Purpose   更新数据
     * @param     tsJzDict
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/dict/update", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo update(@Valid TsJzDict tsJzDict, BindingResult result, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = dictService.updateTsJzDict(tsJzDict, request);
        }
        return retInfo;
    }
    /**
     * @Purpose   添加数据
     * @param     tsJzDict
     * @version   1.0
     * @author    李准
     * @time      2017-08-19
     * @return    RetInfo
     */
    @RequestMapping(value = "/dict/add", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    @Authority(AuthorityType.Login)
    public RetInfo insert(@Valid TsJzDict tsJzDict, BindingResult result, HttpServletRequest request) {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors()) {
            List<FieldError> err = result.getFieldErrors();
            FieldError fe = err.get(0);
            retInfo.setMark("1");
            retInfo.setTip(fe.getDefaultMessage());
        } else {
            retInfo = dictService.insertTsJzDict(tsJzDict, request);
        }
        return retInfo;
    }
    /**
     * @Purpose  跳转到添加数据页面
     * @version  1.0
     * @author   lizhun
     * @param    request
     * @return   RetInfo
     */
    @RequestMapping(value="/dict/to/add")
    @Authority(AuthorityType.Login)
    public ModelAndView index(HttpServletRequest request){
        ModelAndView mv = new ModelAndView("dict/add");
        mv.addObject("token", request.getParameter("token"));
        mv.addObject("code_type", request.getParameter("code_type"));
        mv.addObject("parent_id", request.getParameter("parent_id"));
        return mv;
    }
}
